package orest.front.market;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import orest.back.userneeds.UserNeedsSql;
import orest.front.market.MarketSql;

import net.ssta.common.Constants;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.HandlerBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;
import net.ssta.core.util.DateUtility;


/**
 * 
 * @ClassName: MarketHandler
 * @Description: 需求市场相关处理
 * @author: zhanggk
 * @date: Feb 29, 2012
 * 
 */
public class MarketHandler extends HandlerBase
{
    
    /**
     * 获取用户提交的技术需求信息
     */
    public RecordSet getTechneedList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getTechneedByUseridSql();
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    /**
     * 删除技术需求信息
     * @param techneedid:技术需求的uuid
     */
    public void techneedDelete(String techneedid)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.deleteTechneedSql(techneedid);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 获取用户提交的技术需求信息
     */
    public RecordSet getTechneedByCondition(String infoclass, String searchday, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getTechneedByConditionSql(infoclass,searchday);
            recordSet = m_executor.find(sql, pagingInfo);
            //System.out.println("+++++++++++++++++"+recordSet);
            if(recordSet!=null){
            	System.out.println(recordSet.size());
            }
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 获取用户提交的自荐项目信息
     */
    public RecordSet getSelloneselfList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getSelloneselfByUseridSql(LoginUser.getUserId());
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 获取用户提交的技术需求信息
     */
    public RecordSet getSelloneselfByCondition(String infoclass, String searchday, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getSelloneselfByConditionSql(infoclass,searchday);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    /**
     * 删除自荐项目信息
     * @param selloneselfid:自荐项目的uuid
     */
    public void selloneselfDelete(String selloneselfid)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.deleteSelloneselfSql(selloneselfid);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 获取用户提交的投融资项目信息
     */
    public RecordSet getInvestList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getInvestByUseridSql(LoginUser.getUserId());
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 获取用户提交的技术需求信息
     */
    public RecordSet getInvestByCondition(String infoclass, String searchday, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getInvestByConditionSql(infoclass,searchday);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    /**
     * 删除投融资项目信息
     * @param investid:投融资项目项目的uuid
     */
    public void investDelete(String investid)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = MarketSql.deleteInvestSql(investid);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 根据uuid获取技术需求的详细信息
     */
    public Record getTechneedById(String uuid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getTechneedInfoSql(uuid);
            recordSet = m_executor.find(sql);
            if(recordSet!=null && recordSet.size()>0){
            	record = recordSet.get(0);
            }
            
            return record;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 根据uuid获取技术需求的详细信息
     */
    public RecordSet getTechneedInfoById(String uuid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getTechneedInfoSql(uuid);
            recordSet = m_executor.find(sql);
            if(recordSet!=null && recordSet.size()>0){
            	record = recordSet.get(0);
            }
            
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 根据uuid获取自荐项目的详细信息
     */
    public Record getSelloneselfById(String uuid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getSelloneselfInfoSql(uuid);
            recordSet = m_executor.find(sql);
            if(recordSet!=null && recordSet.size()>0){
            	record = recordSet.get(0);
            }
            
            return record;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 根据uuid获取投融资项目的详细信息
     */
    public Record getInvestById(String uuid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getInvestInfoSql(uuid);
            recordSet = m_executor.find(sql);
            if(recordSet!=null && recordSet.size()>0){
            	record = recordSet.get(0);
            }
            return record;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 从T_FieldOption获取字段选项
     */
    public RecordSet getFildOption(String tablename,String fieldname)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = MarketSql.getFildOption(tablename,fieldname);
            recordSet = m_executor.find(sql);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    public static String getCalculateDate(int iyear,int imonth,int iday) {
        Calendar c = Calendar.getInstance();
        //当前日期
        c.setTime(new Date(System.currentTimeMillis()));
        int day = c.get(Calendar.DATE);
        int month = c.get(Calendar.MONTH);
        int year = c.get(Calendar.YEAR);
        
        c.set(Calendar.YEAR, year+iyear);
        c.set(Calendar.MONTH, month+imonth);
        c.set(Calendar.DATE, day+iday);
        
        String returndate = DateUtility.getString(c.getTime(), "yyyy-MM-dd");
        //System.out.println(returndate);
        return returndate;
    }
    
    /**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		getCalculateDate(0,0,-7);
	}
    
}

